package com.sl.au.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sl.au.entity.BusinesssellerAuthentication;
import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.repository.BusinesssellerBaseInfoRepository;
import com.sl.au.repository.BusinesssellerAuthenticationRepository;
import com.sl.au.service.BusinesssellerBaseInfoService;

@Service
@Transactional
public class BusinesssellerBaseInfoServiceImpl extends BaseServiceImpl implements BusinesssellerBaseInfoService {

	@Autowired
	private BusinesssellerBaseInfoRepository businesssellerBaseInfoRepository;
    
	@Autowired
	private BusinesssellerAuthenticationRepository businesssellerAuthenticationRepository;
	@Override
	public BusinesssellerBaseInfoRepository getRepository()
	{
	   return	this.businesssellerBaseInfoRepository;
	}
	@Override
	public int savepicture(String url,String name){
		int code =-1;
		BusinesssellerBaseInfo binfo= this.businesssellerBaseInfoRepository.getInfoByName(name);
		if(binfo!=null){
			BusinesssellerAuthentication ba =this.businesssellerAuthenticationRepository.getInfoByBaseInfoId(binfo.getId());
			binfo.setPictureAdress(url);
			this.businesssellerBaseInfoRepository.save(binfo);
			if(ba!=null){
				ba.setPictureAdress(url);
				this.businesssellerAuthenticationRepository.save(ba);
			}
		}
		code=1;
		
		return code;
	}
	@Override
	public List<BusinesssellerBaseInfo> getPage(int start, int length, String key, String orderName, Direction dir) {
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if(StringUtils.hasText(key)){
			pages=this.businesssellerBaseInfoRepository.getByKey(key,pageable);
		}
		else{
			pages = businesssellerBaseInfoRepository.findAll(pageable);
		}
		
		return pages.getContent();
	}
	
	@Override
	public List<BusinesssellerBaseInfo> getPage(int start, int length, String key, String level, String orderName, Direction dir) {
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if(StringUtils.hasText(key) && StringUtils.hasText(level)){
			pages=this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, pageable);
		}else if(StringUtils.hasText(key) && !StringUtils.hasText(level)){
			pages=this.businesssellerBaseInfoRepository.getByKey(key,pageable);
		}else if(!StringUtils.hasText(key) && StringUtils.hasText(level)){
			pages=this.businesssellerBaseInfoRepository.getByLevel(level, pageable);
		}else{
			pages = this.businesssellerBaseInfoRepository.findAll(pageable);
		}
		
		return pages.getContent();
	}
	
	@Override
	public List<BusinesssellerBaseInfo> getPageByLevel(int start, int length, String key, String level, String orderName, Direction dir) {
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if(StringUtils.hasText(key)){
			pages=this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, pageable);
		}else {
			pages=this.businesssellerBaseInfoRepository.getByLevel(level, pageable);
		}
		
		return pages.getContent();
	}
	
	@Override
	public int getCount(String key) {
		if (!StringUtils.hasText(key))
			return this.businesssellerBaseInfoRepository.findAll().size();
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		return businesssellerBaseInfoRepository.getByKey(key, pageable).getContent().size();
	}
	
	@Override
	public int getCount(String key, String level) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if(StringUtils.hasText(key) && StringUtils.hasText(level)){
			return this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, pageable).getContent().size();
		}else if(StringUtils.hasText(key) && !StringUtils.hasText(level)){
			return this.businesssellerBaseInfoRepository.getByKey(key,pageable).getContent().size();
		}else if(!StringUtils.hasText(key) && StringUtils.hasText(level)){
			return this.businesssellerBaseInfoRepository.getByLevel(level, pageable).getContent().size();
		}else{
			return this.businesssellerBaseInfoRepository.findAll(pageable).getContent().size();
		}
	}
	
	
	@Override
	public int getCountByLevel(String key, String level) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if(StringUtils.hasText(key)){
			return this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, pageable).getContent().size();
		}else{
			return this.businesssellerBaseInfoRepository.getByLevel(level, pageable).getContent().size();
		}
	}
	
	@Override
	public List<BusinesssellerBaseInfo> findAll() {

		return businesssellerBaseInfoRepository.findAll();
	}

	@Override
	public BusinesssellerBaseInfo findByBussinessSellerId(String bussinessId) {

		return businesssellerBaseInfoRepository.findOne(bussinessId);
	}

	@Override
	public List<BusinesssellerBaseInfo> getPageByNoLevel(int start, int length, String key, String orderName,
			Direction dir) {
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if(StringUtils.hasText(key)){
			pages=this.businesssellerBaseInfoRepository.getNoLevelByKey(key,pageable);
		}
		else{
			pages = businesssellerBaseInfoRepository.findNoLevelAll(pageable);
		}
		
		return pages.getContent();
	}

	@Override
	public int getCountByNoLevel(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if(StringUtils.hasText(key)){
			return this.businesssellerBaseInfoRepository.getNoLevelByKey(key,pageable).getContent().size();
		}else{
			return this.businesssellerBaseInfoRepository.findNoLevelAll(pageable).getContent().size();
		}
	}
	@Override
	public void deleteBussinessLevel(String[] ids) {
		if (ids.length > 0) {
			for(String bussinessLevelId :ids) {
				businesssellerBaseInfoRepository.setBusinesssellerBaseInfo(bussinessLevelId);
			}
		}
		
	}
}
